package GameData;
import implementation.*;
import java.util.Calendar;

public class Game {
	public String awayTeam;
	public String homeTeam;
	public String dayOfWeek;
	public int dateMonth;
	public int dateDay;
	public String time;
	public TicketAggregate ticketAggregate;
	
	public int bestTicketPrice;
	public int cheapestTicketPrice;
	
	public Game(String awayT, String homeT, String dow, 
			int dMonth, int dDay, String t)
	{
		awayTeam = awayT.toLowerCase();
		homeTeam = homeT.toLowerCase();
		dayOfWeek = dow;
		dateMonth = dMonth;
		dateDay = dDay;
		time = t;
		
		bestTicketPrice = new Long(Math.round(Math.random()*50.0)).intValue() + 600;
		cheapestTicketPrice = new Long(Math.round(Math.random()*5.0)).intValue() + 5;
	}
	
	public String toString()
	{
		return "Game: " + homeTeam + "(h) vs " + awayTeam + "(a) on " + 
			dayOfWeek + ", " + Games.numToMonthConverter.get(dateMonth) + 
			" " + dateDay + " at " + time + ".";
	}
	
	public String getDate()
	{
		return Games.numToMonthConverter.get(dateMonth) + " " + dateDay;
	}
	
	public String getDateAsNums()
	{
		return dateMonth + " " + dateDay;
	}
	
	public boolean hasAnyTickets(int numTickets)
	{
		int todayMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
		int todayDay = Calendar.getInstance().get(Calendar.DATE);
		
		//verify that is later than the game's date
		if(dateMonth < todayMonth)
			return false;			
		if(dateMonth < todayMonth && dateDay < todayDay)
			return false;
		if(numTickets > 500)
			return Math.random()*1000 > 950 ? true : false;
		if(numTickets > 250)
			return Math.random()*1000 > 900 ? true : false;
		if(numTickets > 50)
			return Math.random()*1000 > 850 ? true : false;
		if(numTickets > 30)
			return Math.random()*1000 > 700 ? true : false;
		if(numTickets > 15)
			return Math.random()*1000 > 600 ? true : false;
			
		//less than "1 week" till game day
		if(dateMonth == todayMonth && todayDay-dateDay<8)
		{
			return Math.random()*1000 > 100 ? true : false;
		}
		//more than "1 week" till game day
		if(dateMonth == todayMonth)
		{
			return Math.random()*1000 > 50 ? true : false;
		}
		return Math.random()*1000 > 10 ? true : false;
	}
	
	public boolean hasSpecificTickets(int numTickets, PriceType pt)
	{
		int todayMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
		int todayDay = Calendar.getInstance().get(Calendar.DATE);
		
		//verify that is later than the game's date
		if(dateMonth < todayMonth)
			return false;			
		if(dateMonth < todayMonth && dateDay < todayDay)
			return false;
		if(numTickets > 500)
			return Math.random()*1000 > 950 ? true : false;
		if(numTickets > 250)
			return Math.random()*1000 > 900 ? true : false;
		if(numTickets > 50)
			return Math.random()*1000 > 850 ? true : false;
		if(numTickets > 30)
			return Math.random()*1000 > 700 ? true : false;
		if(numTickets > 15)
			return Math.random()*1000 > 600 ? true : false;
			
		if(dateMonth == todayMonth && todayDay-dateDay<8) //less than "1 week" till game day
		{
			if(pt.equals(PriceType.BEST))
				return Math.random()*1000 > 300 ? true : false;
			if(pt.equals(PriceType.BEST_AVAILABLE))
				return Math.random()*1000 > 250 ? true : false;
			if(pt.equals(PriceType.MIDRANGE))
				return Math.random()*1000 > 200 ? true : false;
			if(pt.equals(PriceType.CHEAPEST_AVAILABLE))
				return Math.random()*1000 > 150 ? true : false;
			if(pt.equals(PriceType.CHEAPEST))
				return Math.random()*1000 > 100 ? true : false;
		}
		if(dateMonth == todayMonth) //more than "1 week" till game day
		{
			if(pt.equals(PriceType.BEST))
				return Math.random()*1000 > 250 ? true : false;
			if(pt.equals(PriceType.BEST_AVAILABLE))
				return Math.random()*1000 > 200 ? true : false;
			if(pt.equals(PriceType.MIDRANGE))
				return Math.random()*1000 > 150 ? true : false;
			if(pt.equals(PriceType.CHEAPEST_AVAILABLE))
				return Math.random()*1000 > 100 ? true : false;
			if(pt.equals(PriceType.CHEAPEST))
				return Math.random()*1000 > 50 ? true : false;
		}
		else
		{
			if(pt.equals(PriceType.BEST))
				return Math.random()*1000 > 200 ? true : false;
			if(pt.equals(PriceType.BEST_AVAILABLE))
				return Math.random()*1000 > 150 ? true : false;
			if(pt.equals(PriceType.MIDRANGE))
				return Math.random()*1000 > 100 ? true : false;
			if(pt.equals(PriceType.CHEAPEST_AVAILABLE))
				return Math.random()*1000 > 50 ? true : false;
			if(pt.equals(PriceType.CHEAPEST))
				return Math.random()*1000 > 5 ? true : false;
		}
		
		
		//should never reach here...
		return true;
	}
	
	/*
	 * seating types are:
	 * - field level
	 * - mezzanine
	 * - upper deck
	 * - bleachers
	 */
	public TicketAggregate getTicketInfo(int numTickets, String priceType)
	{
		String pt = priceType.toLowerCase().trim();
		TicketAggregate ta = new TicketAggregate();
		Double rando = Math.random()*1000;
		System.out.println("/////////////////////");
		System.out.println("price type: " + pt + ", random: " + rando);
		System.out.println("/////////////////////");
		//Double probField=0.0, probMezz=0.0, probUpper=0.0, probBleach = 0.0;
		
		//best (available)
		//midrange or mid range or middle range
		//cheapest (available)
		
		boolean modified = false;
		Double midRandom = Math.random()*100.0;
		if(pt.equals("best") || pt.equals("best available"))
		{
			ta.section = "field level";
			ta.numTickets = numTickets;
			ta.sectionNum = new Long(Math.round(Math.random()*29.0)).intValue()+1;//1-30
			ta.costPerTicket = bestTicketPrice;//$600-$650
			ta.totalCost = ta.numTickets * ta.costPerTicket;
			modified = true;
		}
		else if(pt.equals("cheapest") || pt.equals("cheapest available"))
		{
			ta.section = "bleachers";
			ta.sectionNum = new Long(Math.round(Math.random()*29.0)).intValue()+91;//91-120
			ta.numTickets = numTickets;
			ta.costPerTicket = cheapestTicketPrice; //$5-$10
			ta.totalCost = ta.numTickets * ta.costPerTicket;
			modified = true;
		}
		else if((pt.contains("best") && ticketAggregate==null) || //best ticket, first search
				(ticketAggregate!=null && pt.contains("best") && ticketAggregate.section.equals("mezzanine"))) //refines tickets to be more expensive 
		{
			ta.section = "field level";
			ta.numTickets = numTickets;
			ta.sectionNum = new Long(Math.round(Math.random()*29.0)).intValue()+1;//1-30
			ta.costPerTicket = new Long(Math.round(Math.random()*450.0)).intValue() + 150;//$150-$600
			ta.totalCost = ta.numTickets * ta.costPerTicket;
			modified = true;
		} 
		else if((pt.contains("mid") && midRandom<50.0) || //mid ticket, first search
				(ticketAggregate!=null && pt.contains("best") && ticketAggregate.section.equals("upper deck")) || //refine tickets to be more expensive
				(ticketAggregate!=null && pt.contains("cheap") && ticketAggregate.section.equals("field level"))) //refine tickets to be cheaper 
		{
			ta.section = "mezzanine";
			ta.numTickets = numTickets;
			ta.sectionNum = new Long(Math.round(Math.random()*29.0)).intValue()+31;//31-60
			ta.costPerTicket = new Long(Math.round(Math.random()*99.0)).intValue() + 50;//$50-$149
			ta.totalCost = ta.numTickets * ta.costPerTicket;
			modified = true;
		}
		else if((pt.contains("mid") && midRandom>=50.0) || //mid ticket, first search
				(ticketAggregate!=null && pt.contains("best") && ticketAggregate.section.equals("bleachers")) || //refine search to be more expensive
				(ticketAggregate!=null && pt.contains("cheap") && ticketAggregate.section.equals("mezzanine"))) //refine search to be cheaper
		{
			ta.section = "upper deck";
			ta.sectionNum = new Long(Math.round(Math.random()*29.0)).intValue()+61;//61-90
			ta.numTickets = numTickets;
			ta.costPerTicket = new Long(Math.round(Math.random()*29.0)).intValue() + 20;//$20-$49
			ta.totalCost = ta.numTickets * ta.costPerTicket;
			modified = true;
		}
		else if((pt.contains("cheap") && ticketAggregate==null) || //cheapest ticket, first search
				(ticketAggregate!=null && pt.contains("cheap") && ticketAggregate.section.equals("upper deck"))) //refine search to be more expensive
		{
			ta.section = "bleachers";
			ta.sectionNum = new Long(Math.round(Math.random()*29.0)).intValue()+91;//91-120
			ta.numTickets = numTickets;
			ta.costPerTicket = new Long(Math.round(Math.random()*9.0)).intValue() + 10; //$10-$19
			ta.totalCost = ta.numTickets * ta.costPerTicket;
			modified = true;
		}
		else
		{
			System.out.println("----------------\nSEARCH CAN NOT BE IMPROVED ON WHAT YOU ALREADY HAVE!\n----------------");
		}
		
		if(modified)
		{
			ticketAggregate = ta;
		}
		return ta;
		
//		if(pt.equals("best") || pt.equals("best available")) {
//			probField = 700.0;
//			probMezz = 900.0;
//			probUpper = 970.0;
//			probBleach = 1000.0;
//		} else if (pt.equals("next best") || pt.equals("next best available")) {
//			probField = 500.0;
//			probMezz = 750.0;
//			probUpper = 900.0;
//			probBleach = 1000.0;
//		} else if (pt.equals("midrange") || pt.equals("mid range") || pt.equals("middle range")) {
//			probField = 100.0;
//			probMezz = 500.0;
//			probUpper = 900.0;
//			probBleach = 1000.0;
//		} else if (pt.equals("next cheapest") || pt.equals("next cheapest available")) {
//			probField = 100.0;
//			probMezz = 250.0;
//			probUpper = 400.0;
//			probBleach = 1000.0;
//		} else if (pt.equals("cheapest") || pt.equals("cheapest available")) {
//			probField = 30.0;
//			probMezz = 70.0;
//			probUpper = 200.0;
//			probBleach = 1000.0;
//		} else {
//			probField = 250.0;
//			probMezz = 500.0;
//			probUpper = 750.0;
//			probBleach = 1000.0;
//		}
//			
//		if(rando < probField ||
//				(ticketAggregate != null && ticketAggregate.sectionNum>30 && pt.contains("best")))
//		{
//			ta.section = "field level";
//			ta.numTickets = numTickets;
//			ta.sectionNum = new Long(Math.round(Math.random())).intValue()*29+1;//1-30
//			ta.costPerTicket = new Long(Math.round(Math.random())).intValue()*450 + 150;//$150-$600
//			ta.totalCost = ta.numTickets * ta.costPerTicket;
//		}
//		if(rando < probMezz ||
//				(ticketAggregate != null && ticketAggregate.sectionNum>60 && pt.contains("best")))
//		{
//			ta.section = "mezzanine";
//			ta.numTickets = numTickets;
//			ta.sectionNum = new Long(Math.round(Math.random())).intValue()*29+31;//31-60
//			ta.costPerTicket = new Long(Math.round(Math.random())).intValue()*99 + 50;//$50-$149
//			ta.totalCost = ta.numTickets * ta.costPerTicket;
//		}
//		if(rando < probUpper ||
//				(ticketAggregate != null && ticketAggregate.sectionNum>90 && pt.contains("best")))
//		{
//			ta.section = "upper deck";
//			ta.sectionNum = new Long(Math.round(Math.random())).intValue()*29+61;//61-90
//			ta.numTickets = numTickets;
//			ta.costPerTicket = new Long(Math.round(Math.random())).intValue()*29 + 20;//$20-$49
//			ta.totalCost = ta.numTickets * ta.costPerTicket;
//		}
//		else
//		{
//			ta.section = "bleachers";
//			ta.sectionNum = new Long(Math.round(Math.random())).intValue()*29+91;//91-120
//			ta.numTickets = numTickets;
//			ta.costPerTicket = new Long(Math.round(Math.random())).intValue()*9 + 10; //$10-$19
//			ta.totalCost = ta.numTickets * ta.costPerTicket;
//		}
	}
}






